package com.loubobooo.personal.blog.controller.backend;

import com.loubobooo.personal.blog.common.Const;
import com.loubobooo.personal.blog.entity.Article;
import com.loubobooo.personal.blog.entity.vo.*;
import com.loubobooo.personal.blog.service.ArticleService;
import com.loubobooo.personal.blog.service.CategoryService;
import com.loubobooo.personal.blog.service.TagService;
import com.loubobooo.personal.blog.util.JsonUtil;
import com.loubobooo.personal.blog.util.RedisPoolUtil;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller @RequestMapping("/admin/article") public class ArticleManageController {
    @Autowired private ArticleService articleService;

    @Autowired private TagService tagService;

    @Autowired private CategoryService categoryService;

    //后台文章列表显示
    @RequestMapping(value = "") public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();

        //分页显示已发布文章
        Integer pageSize = 10;
        List<ArticleListVo> publishedArticleListVoList = null;
        String cacheValue = RedisPoolUtil.get(Const.REDIS_KEY.ALL_ARTICLE);
        if(StringUtils.isBlank(cacheValue)){
            publishedArticleListVoList = articleService.listArticleByPage(1, null, pageSize);
            RedisPoolUtil.set(Const.REDIS_KEY.ALL_ARTICLE, JsonUtil.obj2String(publishedArticleListVoList));
        }else{
            publishedArticleListVoList = JsonUtil.string2Obj(RedisPoolUtil.get(Const.REDIS_KEY.ALL_ARTICLE),
                    new TypeReference<List<ArticleListVo>>(){});
        }
        modelAndView.addObject("publishedArticleListVoList", publishedArticleListVoList);

        //不分页显示 草稿文章
        List<ArticleListVo> draftArticleList = articleService.listArticle(0);
        modelAndView.addObject("draftArticleList", draftArticleList);
        modelAndView.setViewName("Admin/Article/index");
        return modelAndView;
    }

    //文章分页显示
    @RequestMapping("/p/{pageNo}")
    public ModelAndView ArticleListByPageView(
            @PathVariable("pageNo") Integer pageNo) {
        ModelAndView modelAndView = new ModelAndView();
        //分页显示已发布文章
        Integer pageSize = 10;

        List<ArticleListVo> publishedArticleListVoList = articleService.listArticleByPage(1, pageNo, pageSize);
        modelAndView.addObject("publishedArticleListVoList", publishedArticleListVoList);

        //不分页显示 草稿文章
        List<ArticleListVo> draftArticleList = articleService.listArticle(0);
        modelAndView.addObject("draftArticleList", draftArticleList);
        modelAndView.setViewName("Admin/Article/index");
        return modelAndView;
    }

    //后台添加文章页面显示
    @RequestMapping(value = "/insert")
    public ModelAndView insertArticleView() {
        ModelAndView modelAndView = new ModelAndView();

        List<CategoryVO> categoryVOList = categoryService.listCategory(1);
        List<TagVO> tagVOList = tagService.listTag(1);

        modelAndView.addObject("categoryVOList", categoryVOList);
        modelAndView.addObject("tagVOList", tagVOList);

        modelAndView.setViewName("Admin/Article/insert");
        return modelAndView;
    }

    //后台添加文章提交操作
    @RequestMapping(value = "/insertSubmit", method = RequestMethod.POST)
    public String insertArticleSubmit(
            Article article) {
        article.setArticleStatus(1);
		article.setArticleContent(article.getArticleContent().replace("&", "&amp;"));

        articleService.insertArticle(article);
        return "redirect:/admin/article";
    }

    //后台添加文章提交操作
    @RequestMapping(value = "/insertDraftSubmit", method = RequestMethod.POST) public String insertArticleDraftSubmit(
            Article article) {
        article.setArticleStatus(0);
		article.setArticleContent(article.getArticleContent().replace("&", "&amp;"));
        articleService.insertArticle(article);
        return "redirect:/admin/article";
    }

    //搜索实现
    @RequestMapping("/search")
    public ModelAndView SearchPageView(HttpServletRequest request,
            Model model) {
        ModelAndView modelAndView = new ModelAndView();
        //设置每页显示的数量
        int pageSize = 10;
        String query = request.getParameter("query");
        List<ArticleSearchVo> articleSearchVoList = articleService
                .listSearchResultByPage(1, request, model, null, pageSize, query);
        modelAndView.addObject("articleSearchVoList", articleSearchVoList);
        modelAndView.setViewName("Admin/Article/search");
        return modelAndView;
    }

    //搜索分页实现
    @RequestMapping("/p/{pageNo}/search")
    public ModelAndView SearchPageByPageView(
            HttpServletRequest request, Model model, @PathVariable("pageNo") Integer pageNo) {
        ModelAndView modelAndView = new ModelAndView();
        //设置每页显示的数量
        int pageSize = 10;
        String query = request.getParameter("query");
        List<ArticleSearchVo> articleSearchVoList = articleService
                .listSearchResultByPage(1, request, model, pageNo, pageSize, query);
        modelAndView.addObject("articleSearchVoList", articleSearchVoList);
        modelAndView.setViewName("/Admin/Article/search");
        return modelAndView;
    }

    //删除文章
    @RequestMapping(value = "/delete/{id}")
    public void deleteArticle(@PathVariable("id") Integer id) {
        //调用service批量删除
        articleService.deleteArticle(id);
    }

    //批量删除文章
    @RequestMapping(value = "/deleteBatch")
    public void deleteArticles(HttpServletRequest request) {
        String str = request.getParameter("ids");
        String[] arr = str.split(",");
        Integer[] ids = new Integer[arr.length];
        for (int i = 0; i < arr.length; i++) {
            ids[i] = Integer.valueOf(arr[i]);
        }
        //调用service批量删除
        articleService.deleteArticleBatch(ids);
    }

    //编辑文章页面显示
    @RequestMapping(value = "/edit/{id}") public ModelAndView editArticleView(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView();

        ArticleVO articleVO = articleService.getArticleById(null, id);
        modelAndView.addObject("articleVO", articleVO);

        List<CategoryVO> categoryVOList = categoryService.listCategory(1);
        modelAndView.addObject("categoryVOList", categoryVOList);

        List<TagVO> tagVOList = tagService.listTag(1);
        modelAndView.addObject("tagVOList", tagVOList);

        modelAndView.setViewName("Admin/Article/edit");
        return modelAndView;
    }

    //编辑文章提交
    @RequestMapping(value = "/editSubmit", method = RequestMethod.POST)
    public String editArticleSubmit(
            ArticleVO articleVO) {
        Integer id = articleVO.getArticleId();
        articleVO.setArticleUpdateTime(new Date());
		articleVO.setArticleContent(articleVO.getArticleContent().replace("&", "&amp;"));
        articleService.updateArticle(id, articleVO);

        return "redirect:/admin/article";
    }

}



